package com.wenlin.myproduction.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.wenlin.myproduction.R;

/**
 * 图片马赛克控件
 *
 * @author cwl
 * @desc 通过GLide下载图片，然后显示马赛克
 * @Email 377979485@qq.com
 * @time 2023/9/27  16:29
 */
public class MosaicView extends View {
    private Bitmap bitmap;//位图
    private Paint paint;//画笔
    private int column = 40;//画方格子时，如果没有在四个方向放大1像素，则在手机屏幕宽度为1280时，column=45就会出现呲线。手机屏幕宽度为1440时，column=100开始呲线。
    private int x, y;//横纵坐标
    private int color;//颜色
    private int alpha, red, green, blue;//透明度以及红、绿、蓝色值
    private int index;//纵向脚标
    private float blockWidth;//方格子宽度
    private float ratio;//bitmap和此控件的宽度比值
    private String strColor;//颜色字符串

    public MosaicView(Context context) {
        this(context, null);
    }

    public MosaicView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);
        paint.setDither(true);
    }

    public void setImageUrl(String url) {
        try {
            Glide.with(getContext()).asDrawable().load(url).error(R.mipmap.ic_image_error).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    bitmap = ((BitmapDrawable) resource).getBitmap();
                    postInvalidate();
                }

                @Override
                public void onLoadFailed(@Nullable Drawable errorDrawable) {
                    if (errorDrawable != null) {
                        bitmap = ((BitmapDrawable) errorDrawable).getBitmap();
                        postInvalidate();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (bitmap != null && !bitmap.isRecycled() && bitmap.getWidth() > 0 && getWidth() > 0) {
            index = 0;
            ratio = bitmap.getWidth() / (float) getWidth();
            blockWidth = getWidth() / (float) column;
            while (blockWidth * index < getHeight()) {
                for (int i = 0; i < column; i++) {
                    x = (int) ((i + 0.5) * blockWidth * ratio);
                    y = (int) ((index + 0.5) * blockWidth * ratio);
                    if (x >= bitmap.getWidth()) x = bitmap.getWidth() - 1;
                    if (y >= bitmap.getHeight()) y = bitmap.getHeight() - 1;
                    color = bitmap.getPixel(x, y);
                    alpha = Color.alpha(color);
                    red = Color.red(color);
                    green = Color.green(color);
                    blue = Color.blue(color);
                    strColor = String.format("#%02x%02x%02x%02x", alpha, red, green, blue);
                    paint.setColor(Color.parseColor(strColor));
                    //画方格子的时候在四个方向上放大1像素
                    canvas.drawRect(i * blockWidth - 1, index * blockWidth - 1, i * blockWidth + blockWidth + 1, index * blockWidth + blockWidth + 1, paint);
                }
                index++;
            }
        }
    }
}
